package study.springboot.redis.support.result;

import java.util.HashMap;
import java.util.Map;

public final class Results {

    private static final Map<String, Object> EMPTY_DATA = new HashMap<>();

    private Results() {
    }

    /**
     * 成功
     */
    public static Result ok() {
        return ok(EMPTY_DATA);
    }

    public static <T> Result ok(T data) {
        return build("0000", "成功", data);
    }

    /**
     * 失败
     */
    public static Result fail() {
        return fail("9999", "系统异常");
    }

    public static Result fail(String code, String desc) {
        return build(code, desc, EMPTY_DATA);
    }

    private static Result build(String code, String desc, Object data) {
        Result result = new Result();
        result.setCode(code);
        result.setDesc(desc);
        if (data == null) {
            data = EMPTY_DATA;
        }
        result.setData(data);
        return result;
    }
}
